<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24
 * Time: 9:39
 */

namespace app\index\server;

use app\index\model\Theme as ThemeModel;
use app\lib\exception\DatabaseException;
use think\Db;

class ThemeServer extends BaseServer
{
    public function getList($params = [])
    {
        $prep_params = $this->prep_param($params);
        try {
            // 总条数
            $count = ThemeModel::count();
            $list = ThemeModel::with(['topicImg', 'headImg'])
                ->order($prep_params['sort'], $prep_params['order'])
                ->limit($prep_params['offset'], $prep_params['limit'])
                ->select();
        } catch (Exception $e) {
            // 有些问题 应该抛出错误异常
           throw new DatabaseException([
               'msg' => $e->getMessage()
           ]);
        }
        return bootstrap_table_return($count, $list);
    }

    public function addOrUpdate($params = [])
    {
        $topic_image_name = $params['topic_image_name'];
        $topic_image_url = $params['topic_image_url'];
        $head_image_name = $params['head_image_name'];
        $head_image_url = $params['head_image_url'];
        Db::startTrans();
        try{
            $topicImg = $this->handle_image($topic_image_name, $topic_image_url, 'theme',  isset($params['primary_key']), '', 'topic_img');
            $headImg = $this->handle_image($head_image_name, $head_image_url, 'theme',  isset($params['primary_key']), '', 'head_img');
            $theme_data = [
                'name' => $params['name'],
                'description' => $params['description'],
                'cpp_hot' => $params['cpp_hot'],
                'topic_img_id' => $topicImg['img_id'],
                'head_img_id' => $headImg['img_id'],
                'status' => $params['status']
            ];
            if(isset($params['primary_key'])){
                $result = ThemeModel::where('id', '=', $params['primary_key'])
                    ->update($theme_data);
            }else{
                $result = ThemeModel::insert($theme_data);
            }
            if(!$result){
                return setMsg(0, '操作theme失败');
            }
            Db::commit();
        }catch(Exception $e){
            Db::rollback();
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '操作theme成功');
    }

    public function detail($id)
    {
        try{
            $detail = ThemeModel::with(['topicImg', 'headImg'])
                ->where('id', '=', $id)
                ->find();
            if(!$detail){
                return setMsg(0, '查询theme失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '查询theme成功', $detail);
    }

    public function changeStatus($id)
    {
        try{
            $info = ThemeModel::where('id', $id)->find();
            $info->status = abs( ($info->status) - 1 );
            $result = $info->save();
            if(!$result){
                return setMsg(0, '修改theme状态失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改theme状态成功');
    }
}